{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Simple tutorial for using TensorFlow to compute a linear regression.\\n\\nParag K. Mital, Jan. 2016'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"Simple tutorial for using TensorFlow to compute a linear regression.\n",
    "\n",
    "Parag K. Mital, Jan. 2016\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% imports\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/heythisischo/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure\n",
      "  \"matplotlib is currently using a non-GUI backend, \"\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEACAYAAACwB81wAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAH5JJREFUeJzt3XuQXOV55/HvIyGhsaSRkXeMMSySgy+UndiMssJy2Vtq\nKRIRSmJIilp5kipX7CkHokBYjDFaoJbZJXYB5ayCIZQEq4QiFV1y8VpgywmoUCdFsmamdEHYlgjY\nzICJ0UxlbS2y2ysJnv2ju0Vrpi/ndJ8+t/59qrpquvvMOe9paZ5+z/M+73vM3RERkfyblXQDREQk\nHgr4IiI9QgFfRKRHKOCLiPQIBXwRkR6hgC8i0iM6DvhmdpGZPWVm3zWz58zsDxts91Uze8HMDpnZ\nZZ0eV0REwjkngn2cBj7v7ofMbAGw38yecPej1Q3M7ErgEnd/n5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H+IJ4EPufhnwAvBfEm5PI0En8cXKzGYBDwC/CnwIGDKzS5NtVUN/TrmdaXYa+Ly7\nfwj4GPAHafw83f3/Aasqf9+XAVeaWZonjd4IfK/VRqkM+MBm4JakG9GMu5+oeTqf8mzi1HH3ve5e\nbdu3gYuSbE8jISbxxe1y4AV3n3D3U8BO4KqE21RXpdT5x0m3oxl3f83dD1V+PgEcAS5MtlX1ufvP\nKj+eS7nAJZX570oHeT3wP1ttm7qAb2afBF5x9+eSbksrZvZHZvYy8NvAf026PQF8FvhW0o3ImAuB\nV2qe/5CUBqisMbOllHvPzyTbkvoqaZKDwGvAk+4+lnSbGqh2kFt+IUVZlhlYk8lcdwC3UU7n1L6X\niFaTztz9DuCOSl73BmAk/lYGmxxnZrcDp9x9ewJNpNKGKCbxSQ6Y2QLgb4Abp10tp0blyniwMu71\ndTP7oLu3TJvEycx+DTjm7ofMrECLeJlIwHf3tfVeN7NfBJYCz5qZUU4/7Dezy919MsYmAo3bWcd2\nYA8JBfxW7TSz36V8ybc6lgY1EOLzTJNXgYtrnl9UeU3aZGbnUA72f+Huu5NuTyvu/n8rk0vXESBP\nHrOPA580s/VAH7DQzB5190/X2zhVKR13/467v8vdf8Hd30P58nkwiWDfipm9t+bp1ZRzkaljZuso\nX+59sjIQlQVpyuOPAe81syVmNhf4FNC0EiJhRro+v3r+DPieu9+XdEMaMbN/Z2aLKj/3Uc46HE22\nVTO5+23ufrG7/wLl/5tPNQr2kLKAX4eT3v+8d5vZYTM7BKyhPEqeRvcDCyivYXTAzB5MukH1NJrE\nlzR3fwO4nnK103eBne6e1i/37cA/A+83s5fN7DNJt2k6M/s48DvA6krJ44FKpyRtLgD2Vf6+nwH+\n3t33JNymjmnilYhIj0h7D19ERCKigC8i0iMU8EVEeoQCvohIj1DAFxHpEQr4IiI9QgFfRKRHKOCL\niPSI/w+536Vu0dYGYwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f8c6c310450>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# %% Let's create some toy data\n",
    "plt.ion()\n",
    "n_observations = 100\n",
    "fig, ax = plt.subplots(1, 1)\n",
    "xs = np.linspace(-3, 3, n_observations)\n",
    "ys = np.sin(xs) + np.random.uniform(-0.5, 0.5, n_observations)\n",
    "ax.scatter(xs, ys)\n",
    "fig.show()\n",
    "plt.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% tf.placeholders for the input and output of the network. Placeholders are\n",
    "# variables which we need to fill in when we are ready to compute the graph.\n",
    "X = tf.placeholder(tf.float32)\n",
    "Y = tf.placeholder(tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% We will try to optimize min_(W,b) ||(X*w + b) - y||^2\n",
    "# The `Variable()` constructor requires an initial value for the variable,\n",
    "# which can be a `Tensor` of any type and shape. The initial value defines the\n",
    "# type and shape of the variable. After construction, the type and shape of\n",
    "# the variable are fixed. The value can be changed using one of the assign\n",
    "# methods.\n",
    "W = tf.Variable(tf.random_normal([1]), name='weight')\n",
    "b = tf.Variable(tf.random_normal([1]), name='bias')\n",
    "Y_pred = tf.add(tf.mul(X, W), b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% Loss function will measure the distance between our observations\n",
    "# and predictions and average over them.\n",
    "cost = tf.reduce_sum(tf.pow(Y_pred - Y, 2)) / (n_observations - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% if we wanted to add regularization, we could add other terms to the cost,\n",
    "# e.g. ridge regression has a parameter controlling the amount of shrinkage\n",
    "# over the norm of activations. the larger the shrinkage, the more robust\n",
    "# to collinearity.\n",
    "# cost = tf.add(cost, tf.mul(1e-6, tf.global_norm([W])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# %% Use gradient descent to optimize W,b\n",
    "# Performs a single step in the negative gradient\n",
    "learning_rate = 0.01\n",
    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.43989\n",
      "1.30686\n",
      "1.18916\n",
      "1.08503\n",
      "0.992879\n",
      "0.911325\n",
      "0.839141\n",
      "0.77524\n",
      "0.718663\n",
      "0.668564\n",
      "0.624192\n",
      "0.584885\n",
      "0.550058\n",
      "0.519194\n",
      "0.491835\n",
      "0.467578\n",
      "0.446063\n",
      "0.426977\n",
      "0.410039\n",
      "0.395001\n",
      "0.381647\n",
      "0.369783\n",
      "0.359238\n",
      "0.349861\n",
      "0.341519\n",
      "0.334094\n",
      "0.327481\n",
      "0.321587\n",
      "0.316332\n",
      "0.311643\n",
      "0.307455\n",
      "0.303713\n",
      "0.300366\n",
      "0.29737\n",
      "0.294686\n",
      "0.292278\n",
      "0.290117\n",
      "0.288175\n",
      "0.286427\n",
      "0.284853\n",
      "0.283433\n",
      "0.282151\n",
      "0.280991\n",
      "0.279942\n",
      "0.27899\n",
      "0.278125\n",
      "0.277339\n",
      "0.276623\n",
      "0.27597\n",
      "0.275373\n",
      "0.274826\n",
      "0.274325\n",
      "0.273865\n",
      "0.273442\n",
      "0.273052\n",
      "0.272693\n",
      "0.27236\n",
      "0.272052\n",
      "0.271767\n",
      "0.271501\n",
      "0.271254\n",
      "0.271024\n",
      "0.27081\n",
      "0.270609\n",
      "0.270421\n",
      "0.270245\n",
      "0.27008\n",
      "0.269925\n",
      "0.269779\n",
      "0.269642\n",
      "0.269512\n",
      "0.26939\n",
      "0.269274\n",
      "0.269165\n",
      "0.269062\n",
      "0.268964\n",
      "0.268871\n",
      "0.268783\n",
      "0.268699\n",
      "0.26862\n",
      "0.268544\n",
      "0.268472\n",
      "0.268404\n",
      "0.268339\n",
      "0.268277\n",
      "0.268218\n",
      "0.268161\n",
      "0.268107\n",
      "0.268056\n",
      "0.268007\n",
      "0.26796\n",
      "0.267916\n",
      "0.267873\n",
      "0.267832\n",
      "0.267794\n",
      "0.267756\n",
      "0.267721\n",
      "0.267687\n",
      "0.267654\n",
      "0.267623\n",
      "0.267594\n",
      "0.267565\n",
      "0.267538\n",
      "0.267512\n",
      "0.267487\n",
      "0.267464\n",
      "0.267441\n",
      "0.267419\n",
      "0.267398\n",
      "0.267378\n",
      "0.267359\n",
      "0.267341\n",
      "0.267324\n",
      "0.267307\n",
      "0.267291\n",
      "0.267276\n",
      "0.267261\n",
      "0.267247\n",
      "0.267234\n",
      "0.267221\n",
      "0.267209\n",
      "0.267197\n",
      "0.267186\n",
      "0.267176\n",
      "0.267165\n",
      "0.267156\n",
      "0.267146\n",
      "0.267137\n",
      "0.267129\n",
      "0.267121\n",
      "0.267113\n",
      "0.267105\n",
      "0.267098\n",
      "0.267092\n",
      "0.267085\n",
      "0.267079\n",
      "0.267073\n",
      "0.267067\n",
      "0.267062\n",
      "0.267057\n",
      "0.267052\n",
      "0.267047\n",
      "0.267043\n",
      "0.267039\n",
      "0.267034\n",
      "0.267031\n",
      "0.267027\n",
      "0.267023\n",
      "0.26702\n",
      "0.267017\n",
      "0.267014\n",
      "0.267011\n",
      "0.267008\n",
      "0.267006\n",
      "0.267003\n",
      "0.267001\n",
      "0.266998\n",
      "0.266996\n",
      "0.266994\n",
      "0.266992\n",
      "0.26699\n",
      "0.266989\n",
      "0.266987\n",
      "0.266985\n",
      "0.266984\n",
      "0.266982\n",
      "0.266981\n",
      "0.26698\n",
      "0.266979\n",
      "0.266978\n",
      "0.266976\n",
      "0.266975\n",
      "0.266974\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f8c0037e890>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# %% We create a session to use the graph\n",
    "n_epochs = 1000\n",
    "with tf.Session() as sess:\n",
    "    # Here we tell tensorflow that we want to initialize all\n",
    "    # the variables in the graph so we can use them\n",
    "    sess.run(tf.initialize_all_variables())\n",
    "\n",
    "    # Fit all training data\n",
    "    prev_training_cost = 0.0\n",
    "    for epoch_i in range(n_epochs):\n",
    "        for (x, y) in zip(xs, ys):\n",
    "            sess.run(optimizer, feed_dict={X: x, Y: y})\n",
    "\n",
    "        training_cost = sess.run(\n",
    "            cost, feed_dict={X: xs, Y: ys})\n",
    "        print(training_cost)\n",
    "\n",
    "        if epoch_i % 20 == 0:\n",
    "            ax.plot(xs, Y_pred.eval(\n",
    "                feed_dict={X: xs}, session=sess),\n",
    "                    'k', alpha=epoch_i / n_epochs)\n",
    "            fig.show()\n",
    "            plt.draw()\n",
    "\n",
    "        # Allow the training to quit if we've reached a minimum\n",
    "        if np.abs(prev_training_cost - training_cost) < 0.000001:\n",
    "            break\n",
    "        prev_training_cost = training_cost\n",
    "fig.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
